package javaDemo06;

public class Queen {
    public static void main(String[] args) {
        int number = 1;//棋子数量
        int a = 0;//单论循环次数
        int b =0;//总循环次数
        String[][] arr = make();//初始化数组
        while (number <= 8) {
            int x = (int) (Math.random() * 8);
            int y = (int) (Math.random() * 8);//创建随机位置
            a++;
            b++;
            System.out.println("第" + b + "次");//输出总次数
            if(a > 120){//若单轮循环超出120，则本轮循环可能无解，随后初始化程序
                number = 1;
                a = 0;
                arr = make();
            }
            if (!arr[x][y].equals("*\t") && !arr[x][y].equals("Q\t")) {//判断位置是否合法
                arr[x][y] = "Q\t";
                number++;
            } else {
                continue;
            }
            fill(arr, x, y);//填充不合法位置
            show(arr);//输出数组
            System.out.println("-----------");

        }


    }


    public static void show(String[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (j == arr.length - 1) {
                    System.out.println(arr[i][j]);
                } else {
                    System.out.print(arr[i][j]);
                }
            }
        }
    }

    public static void fill(String[][] arr, int x, int y) {
        for (int j = 0; j < arr.length; j++) {//横向
            if (j != y) {
                arr[x][j] = "*\t";
            }
        }
        for (int i = 0; i < arr.length; i++) {//竖向
            if (i != x) {
                arr[i][y] = "*\t";
            }
        }
        int indexx = x;
        int indexy = y;
        while (x > 0 && y > 0) {
            x--;
            y--;
            arr[x][y] = "*\t";
        }
        x = indexx;
        y = indexy;

        while (x < 7 && y < 7) {
            x++;
            y++;
            arr[x][y] = "*\t";
        }
        x = indexx;
        y = indexy;
        while (x < 7 && y > 0) {
            x++;
            y--;
            arr[x][y] = "*\t";
        }
        x = indexx;
        y = indexy;
        while (x > 0 && y < 7) {
            x--;
            y++;
            arr[x][y] = "*\t";
        }


    }

    public static String[][] make() {
        String[][] arr = new String[8][8];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                arr[i][j] = "$";
            }
        }
        return arr;
    }
}